package com.newtype.meeting.client.widget.frontend;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.model.Account;
import com.newtype.meeting.client.model.MeetingRecord;

/**
 * 维护会议记录窗口
 * @author 汪恭泽
 * Jun 17, 2011 10:22:21 AM
 */
public class MeetingRecordWindow extends Window {
	private BeanModel recordBeanModel;
	private HtmlEditor content;
	//获取当前登录用户
	private Account currentAccount = Registry.get(GMeeting.ACCOUNT);

	public MeetingRecordWindow(MeetingRecord record) {
		setSize(655, 480);
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);
		setModal(true);

		if (record.getAccountID() == null) {
			setHeading("维护会议记录");
			createFormPanel(record);
		} else if (record.getAccountID() != null) {
			if (currentAccount.getAccountID().equals(record.getAccountID())) {
				createFormPanel(record);
			} else {
				createContentPanel(record);
			}
		}
	}

	private void createContentPanel(MeetingRecord record) {
		DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
		ContentPanel cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setStyleAttribute("padding", "10px");
		cp.setBodyBorder(false);
		cp.setFrame(true);

		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb
				.append("<p style='font-size:18px;font-weight: bold;color:#0060A0;'>会议记录内容</p>");
		sb.append("<p>" + record.getFullName());
		if (record.getSubmitDATE() != null) {
			sb.append("&nbsp;(" + dtf.format(record.getSubmitDATE()) + ")");
		}
		sb.append("</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");

		Text titleTxt = new Text(sb.toString());
		titleTxt.setStyleAttribute("background", "white");
		titleTxt.setStyleAttribute("padding", "5px");
		cp.add(titleTxt);

		Text subjectTxt = new Text("主旨∶" + record.getSubject());
		subjectTxt.setStyleAttribute("background", "white");
		subjectTxt.setStyleAttribute("padding", "5px");
		cp.add(subjectTxt);

		Text contentTxt = new Text(record.getContent());
		contentTxt.setStyleAttribute("background", "white");
		contentTxt.setStyleAttribute("padding", "5px");
		contentTxt.setBorders(true);
		cp.add(contentTxt);

		add(cp);
	}

	private void createFormPanel(MeetingRecord record) {
		FormData formData = new FormData("95%");
		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBodyBorder(false);
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);

		TextField<String> subject = new TextField<String>();
		subject.setFieldLabel("主旨");
		subject.setName("subject");
		subject.setAllowBlank(false);
		subject.getMessages().setBlankText("栏位不可为空白");
		formPanel.add(subject, formData);

		content = new HtmlEditor();
		content.setFieldLabel("报告内容");
		content.setName("content");
		content.setValue(record.getContent());
		formPanel.add(content, formData);

		recordBeanModel = BeanModelLookup.get().getFactory(MeetingRecord.class)
				.createModel(record);

		FormBinding formBinding = new FormBinding(formPanel, true);
		formBinding.bind(recordBeanModel);

		formPanel.addButton(getSaveButton());
		if (record.getId() != null && !"".equals(record.getId())) {
			formPanel.addButton(getDeleteButton(record));
		}
		add(formPanel);
	}

	private Button getDeleteButton(final MeetingRecord record) {
		return new Button("删除", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.DeleteMeetingRecord, record);
				hide();
			}
		});
	}

	private Button getSaveButton() {
		return new Button("送出", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MeetingRecord record = recordBeanModel.getBean();
				record.setContent(content.getValue());
				Dispatcher.forwardEvent(AppEvents.SaveorUpdateMeetingRecord,
						record);
				hide();
			}
		});
	}
}
